<template>
  <div class="login-page">
    <van-nav-bar
      class="title"
      title="账号注册"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ pattern: /^\w{5,}$/, message: '用户名不得少于5位' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ pattern: /^\w{6,}$/, message: '密码不得少于6位' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
      <router-link class="link" to="/register"
        >还没有账号,去注册~~~</router-link
      >
    </van-form>
  </div>
</template>
<script>
import { register } from '../api/user'
import { setToken } from '../utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    onClickLeft () {
      // 返回上一级
      this.$router.push('my')
    },
    async onSubmit (values) {
      const res = await register(values)

      setToken(res.body.token)
      this.$toast.success(res.description)
      this.$router.push('/my')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #333;
  font-size: 12px;
  padding-right: 120px;
  float: right;
}
</style>
